﻿SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[sp_GetUserForRest]
    @CodeStashToken		NVARCHAR(MAX),
    @Email				NVARCHAR(256),
	@Password			NVARCHAR(128)
AS
BEGIN
	-- If @CodeStashToken is NOT provided, assume we are using
	-- email/password
    IF( @CodeStashToken IS NULL )

		SELECT	 m.[ApplicationId]
				,m.[UserId]
				,m.[Email]
				,m.[LoweredEmail]
				,u.[UserName]
				,u.[LoweredUserName]
		FROM		[dbo].[aspnet_Membership] m inner join
					[dbo].[aspnet_Users] u ON m.[UserId] = u.[UserId]
		WHERE		m.[LoweredEmail] = LOWER(@Email) AND
					m.PassWord = @Password


    ELSE


		SELECT	 m.[ApplicationId]
				,m.[UserId]
				,m.[Email]
				,m.[LoweredEmail]
				,u.[UserName]
				,u.[LoweredUserName]
		FROM		[dbo].[aspnet_Membership] m inner join
					[dbo].[aspnet_Users] u ON m.[UserId] = u.[UserId]
		WHERE		m.[LoweredEmail] = LOWER(@Email) AND
					m.PassWord = @CodeStashToken


		IF (@@rowcount = 0)
			RETURN(1)
		RETURN(0)
END
